<?php
/**
 * Created by lzl
 * Date: 2021 2021/8/27
 * Time: 14:08
 */
namespace App\Forms;

use App\Exceptions\RequestException;
use App\Models\Admin;
use App\Models\AdminGroup;
use App\Models\GroupMenuRelation;
use App\Services\Admin\AuthServices;

class AdminForms extends BaseForm
{

    /**
     * @param $validated
     * @return bool
     * @throws RequestException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function login($validated)
    {

        $admin = Admin::where('name',$validated['name'])
            ->where('password', encrypt_psd($validated['password']))
            ->first();

        // 登录
        if ($admin instanceof Admin) {

            $admin->update([
                'last_login_time' => date('Y-m-d H:i:s'),
                'last_login_ip' => \Request::getClientIp(),
            ]);

            $services =  new AuthServices();

            $services->admin = $admin;

            return $services->login($validated['remember'] ?? false);

        }

        throw new RequestException(trans('message.auth.login_failed'));

    }


    /**
     * @param $validated
     * @return mixed
     */
    public function getManagerList($validated)
    {
        $where = [];

        if(isset($validated['name'])){
            $where['name'] = $validated['name'];
        }

        if(isset($validated['phone'])){
            $where['phone'] = $validated['phone'];
        }

        $list = Admin::where($where)
            ->page($validated['page'], $validated['limit'])
            ->orderBy('id','desc')
            ->get();
        return $list;
    }


    /**
     * @param $validated
     * @return mixed
     */
    public function getGroupList($validated)
    {
        $list = AdminGroup::where([])
            ->page($validated['page'], $validated['limit'])
            ->orderBy('id','desc')
            ->get();
        return $list;
    }


    /**
     * @return \Illuminate\Support\Collection
     */
    public function getGroupListNoPage()
    {
        $list = AdminGroup::where([])
            ->orderBy('id','desc')
            ->get();
        return $list;
    }

    /**
     * 修改密码
     * @param $validated
     * @return bool|int
     * @throws RequestException
     */
    public function editPassword($validated)
    {

        if(auth('admin')->user()->password != encrypt_psd($validated['password'])){
            throw new RequestException(trans('message.auth.password_error'));
        }

        return Admin::whereKey(auth('admin')->id())->update(
            ['password' => encrypt_psd($validated['new_password'])]
        );
    }


    /**
     * @param $validated
     * @return bool|mixed|null
     */
    public function deleteManager($validated)
    {
        return Admin::whereKey($validated['id'])->delete();
    }


    /**
     * @param $validated
     * @return bool|mixed|null
     * @throws RequestException
     */
    public function deleteGroup($validated)
    {
        if(Admin::where('gid',$validated)->exists()){
            throw new RequestException(trans('message.auth.un_delete_error'));
        }

        return AdminGroup::whereKey($validated['id'])->delete() && GroupMenuRelation::where('gid', $validated['id'])->delete();
    }


    /**
     * @param $validated
     * @return Admin|\Illuminate\Database\Eloquent\Model
     */
    public function editManager($validated)
    {
        if(isset($validated['password']) && filled($validated['password'])){
            $validated['password'] =  encrypt_psd($validated['password']);
        }

        return Admin::updateOrCreate(['id' => $validated['id'] ?? 0 ], $validated);
    }


    /**
     * @param $validated
     * @return Admin|\Illuminate\Database\Eloquent\Model
     */
    public function getManagerOrNew($validated)
    {
        return Admin::firstOrNew(['id' => $validated['id'] ?? 0]);
    }


    /**
     * @param $validated
     * @return AdminGroup|\Illuminate\Database\Eloquent\Model
     */
    public function getGroupOrNew($validated)
    {
        return AdminGroup::firstOrNew(['id' => $validated['id'] ?? 0]);
    }

}